<template>
<div  class='article-container'>
    <el-card  class="publish-container">
    <!-- 状态栏 -->
    <div slot="header" class="clearfix">
        <!-- 3.使用组件 -->
     <my-breadcrumb>添加文章</my-breadcrumb>
</div>
<el-form  label-width="80px"
 ref='myForm'
:rules='rules'
:model='form'
>
     <el-form-item label="标题" prop='title' required>
    <el-input value='' v-model='form.title'></el-input>
  </el-form-item>
  <!-- 内容 -->
  <el-form-item label="内容"  prop='content' required>
   <quill-editor v-model="form.content" :options="editorOption"></quill-editor>
</el-form-item>
  <!-- 封面 -->
   <el-form-item label="封面">
       <el-radio-group v-model="form.coverType" @change='hImageChange'>
        <el-radio  :label="1">单图</el-radio>
  <el-radio  :label="2">三图</el-radio>
   <el-radio  :label="0">无图</el-radio>
  <el-radio  :label="-1">自动</el-radio>
   </el-radio-group>
       </el-form-item>
       <el-form-item  v-if='form.coverType>0'>
           <el-row>
              <el-col :span='6' v-for="(item, index) in form.coverType"  :key='item'>
                  {{index}}
              <my-cover  v-model='form.coverImages[index]'   />
              </el-col>
           </el-row>
           </el-form-item>
  <!-- 频道 -->
   <MyChannel  v-model='form.channelId'></MyChannel>
  <!-- 按钮 -->
  <el-form-item >
       <el-button type="primary" @click='hSubmit(false)'>发表</el-button>
       <el-button plain @click='hSubmit(true)'>存入草稿</el-button>
      </el-form-item>
    </el-form>
 </el-card>
    </div>
</template>
<script>
// 1。导入首页的插件
import MyBreadcrumb from '@/components/breadcrumb.vue'
import MyChannel from '@/components/channel.vue'
// 以下3个quill开头的是富文本导入的样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 富文本导入的组件
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'AddArticle',
  props: ['value'],
  //   2.注册组件
  components: {
    MyBreadcrumb,
    MyChannel,
    // 富文本注册组件
    quillEditor
  },
  data () {
    return {
      // 富文本配置对象
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      form: {
        title: '所念皆星河', // 标题
        channelId: '', // 频道
        content: '叮叮叮叮当 字数够了吧', // 内容
        coverType: '', // 封面图类型  默认无图
        coverImages: [] // 封面图地址
      },
      //  验证规则
      rules: {
        //   标题验证规则
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        // 内容验证规则
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { min: 8, max: 20, message: '长度在 8 到 20 个字符', trigger: 'blur' }
        ],
        // 频道验证规则
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      }
    }
  },
  //   默认 ： 非草稿
  methods: {
    //   图片类型的切换
    hImageChange () {
      if (this.form.coverType === 1) {
        this.form.coverImages.length = 1
      } else if (this.form.coverType === 2) {
        this.form.coverImages.length = 3
      } else {
        this.form.coverImages = []
      }
    },
    hSubmit (draft = false) {
      // 验证
      this.$refs.myForm.validate(valid => {
        if (valid) {
          this.doSubmit(draft)
        }
      })
    },
    async doSubmit (draft) {
      console.log(draft, 'draft')
      try {
        const { title, content, channelId, coverImages, coverType } = this.form
        const res = await this.$http({
          url: ' /mp/v1_0/articles',
          method: 'POST',
          params: {
            draft
          },
          data: {
            title,
            content,
            channel_id: channelId,
            cover: {
              type: coverType,
              image: coverImages
            }
          }
        })
        console.log(res)
        this.$message.success('添加成功')
      } catch (err) {
        console.log(err)
        this.$message.error('添加失败')
      }
    }
  }
}
</script>
<style lang="less" scoped>
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
.publish-container /deep/ .ql-editor{
  height: 300px;
}
.publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
